package com.spark.trouble

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.google.gson.Gson
import com.spark.colliery.common.ext.gone
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.helper.MutableStatePagerAdapter
import com.spark.trouble.handle.LimitHandleFragment
import com.spark.trouble.handle.MajorRiskFragment
import com.spark.trouble.handle.SpotHandleFragment
import com.spark.trouble.model.HTHistoryModel
import kotlinx.android.synthetic.main.act_handle.*
import me.yokeyword.fragmentation.SupportFragment

/**
 * 隐患历史详情
 * [任务类型]
 * 1 -> 标准化检查 -> 主任务
 * 2 -> 标准化检查 -> 子任务
 * 3 -> 借阅申请
 * 4 -> 文件传输
 * 5 -> 归档申请
 * 6 -> 隐患 -> 主任务
 * 7 -> 隐患 -> 子任务(未分配)
 * 8 ->     -> 现场治理
 * 9 ->     -> 限时整改
 * 10 ->    -> 挂牌督办
 * 11 ->    -> 隐患升级
 * 12 ->    -> 重大隐患
 * @info 处理流程状态
 *  1 -> 待处理
 *  2 -> 审核中/待审核
 *  3 -> 被驳回
 *  4 -> 确认中/待确认
 *  5 -> 整改中/待整改
 *  6 -> 验收中/待验收
 *  7 -> 验收驳回
 *  8 -> 待存储
 *  9 -> 已存储
 *  10 -> 已归档
 */
class HTHistoryDetailFragment : BaseFragment() {
    private lateinit var htHistoryDetailAdapter: MutableStatePagerAdapter

    companion object {
        fun newInstance(data: String): HTHistoryDetailFragment {
            val target = HTHistoryDetailFragment()
            val args = Bundle()
            args.putString("data", data)
            target.arguments = args
            return target
        }
    }

    override fun contentResId(): Int {
        return R.layout.act_handle
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { activity?.finish() }
        arguments?.apply {
            getString("data")?.apply {
                val prop = this
                Gson().fromJson(this, HTHistoryModel.ResultMap.Page.Result::class.java)?.apply {
                    val tabTitles = mutableListOf<String>()
                    val frags = mutableListOf<Fragment>()
                    LogUtils.e("【taskType】$taskType 【state】$state 【task_id】$taskId")
                    when (taskType) {
                    // 未分配任务
                        "7" -> {
                        }
                    // 现场治理
                        "8" -> {
                            when (this.state) {
                            // 审核中/待审核
                                "2" -> {
                                }
                            // 被驳回
                                "3" -> {
                                }
                            // 待存储
                                "4" -> {
                                }
                            }
                            tabTitles.apply {
                                add("现场治理")
                                add("现场情况")
                            }
                            frags.apply {
                                add(SpotHandleFragment.newInstance(Const.htTypeHistory, state))
                                add(HTInfoFragment.newInstance(Const.htTypeHistory, "$taskId", taskType))
                            }
                        }
                    // 限时整改
                    // 挂牌督办
                        "9", "10" -> {
                            when (this.state) {
                            // 审核中，待审核
                            // 被驳回
                            // 确认中/待确认
                            // 整改中/待整改
                                "2", "3", "4", "5" -> {
                                    tabTitles.apply {
                                        add("限时整改")
                                        add("现场情况")
                                    }
                                    frags.apply {
                                        add(LimitHandleFragment.newInstance(Const.htTypeHistory, state, taskType))
                                        add(HTInfoFragment.newInstance(Const.htTypeHistory, "$taskId", taskType))
                                    }
                                }
                            // 验收中/待验收
                                "6" -> {
                                    tabTitles.apply {
                                        add("整改情况")
                                        add("限时整改")
                                        add("现场情况")
                                    }
                                    frags.apply {
                                        add(RectifyFragment.newInstance("$taskId"))
                                        add(LimitHandleFragment.newInstance(Const.htTypeHistory, state, taskType))
                                        add(HTInfoFragment.newInstance(Const.htTypeHistory, "$taskId", taskType))
                                    }
                                }
                            // 验收驳回
                                "7" -> {
                                    tv_title.text = "待整改"
                                    tabTitles.apply {
                                        add("驳回意见")
                                        add("整改通知书")
                                        add("现场情况")
                                    }
                                    frags.apply {
                                        add(RejectOpinionFragment.newInstance())
                                        add(LimitHandleFragment.newInstance(Const.htTypeHistory, state, taskType))
                                        add(HTInfoFragment.newInstance(Const.htTypeHistory, "$taskId", taskType))
                                    }
                                }
                            // 待存储
                                "8" -> {
                                    tabTitles.apply {
                                        add("验收情况")
                                        add("整改情况")
                                        add("限时整改")
                                        add("现场情况")
                                    }
                                    frags.apply {
                                        add(LimitHandleFragment.newInstance(Const.htTypeHistory, state, taskType))
                                        add(LimitHandleFragment.newInstance(Const.htTypeHistory, state, taskType))
                                        add(HTInfoFragment.newInstance(Const.htTypeHistory, "$taskId", taskType))
                                    }
                                }

                            }
                        }
                    // 隐患升级
                        "11" -> {
                            tabTitles.add("重大隐患")
                            tv_title.text = "重大隐患"
                            tabs.gone(true)
                            frags.add(MajorRiskFragment.newInstance(state))
                        }
                    // 重大隐患[重大治理任务]
                        "12" -> {
                            // 2 审核中
                            // 3 被驳回
                            tabTitles.apply {
                                add("重大隐患")
                                add("现场情况")
                            }
                            frags.apply {
                                add(MajorRiskFragment.newInstance(state))
                                add(HTInfoFragment.newInstance(Const.htTypeHistory, "$taskId", taskType))
                            }
                        }
                    }
                    // end of when
                    tabTitles.forEach {
                        tabs.addTab(tabs.newTab().setText(it))
                    }
                    htHistoryDetailAdapter = object : MutableStatePagerAdapter(childFragmentManager) {
                        override fun getCount(): Int {
                            return tabTitles.size
                        }

                        override fun getPageTitle(position: Int): CharSequence? {
                            return tabTitles[position]
                        }
                    }
                    htHistoryDetailAdapter.adds(frags)
                    pager.apply {
                        adapter = htHistoryDetailAdapter
                        canScroll = false
                    }
                    tabs.setupWithViewPager(pager)
                }
            }
        }
    }

    override fun onFragmentResult(requestCode: Int, resultCode: Int, data: Bundle?) {
        super.onFragmentResult(requestCode, resultCode, data)
        htHistoryDetailAdapter.pageList[pager.currentItem].fragment.let { it as SupportFragment }.onFragmentResult(requestCode,resultCode,data)
    }
}